/*
 * 文件名：BpaOutcaeSureServiceImpl.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年4月16日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.service.impl;


import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.infopub.mgr.bpa.dao.OutcaeSureDao;
import top.infopub.mgr.bpa.dao.OutcaeSureDetailDao;
import top.infopub.mgr.bpa.domain.OutcaeSureDetailDomain;
import top.infopub.mgr.bpa.service.BpaOutcaeSureService;
import top.infopub.model.bpa.OutcaeSure;

import com.google.common.collect.Maps;


@Service
public class BpaOutcaeSureServiceImpl implements BpaOutcaeSureService {

    @Autowired
    private OutcaeSureDao outcaeSureDao;

    @Autowired
    private OutcaeSureDetailDao outcaeSureDetailDao;

    @Override
    public void bpaOutcaeSure(String proCode) {
        Map<String, Object> map = Maps.newHashMap();
        List<OutcaeSureDetailDomain> outcDomains = outcaeSureDetailDao.queryByProCode(proCode);
        for (OutcaeSureDetailDomain oDomain : outcDomains) {
            //操作detail明细表
            map.put("subName", oDomain.getSubName());
            map.put("refId", oDomain.getRefExtIdBpa());
            map.put("typeName", oDomain.getTypeName());
            map.put("sureMonthBpa", oDomain.getSureMonthBpa());
            //下浮前
            BigDecimal addupAmountPre = outcaeSureDetailDao.selectAddupAmountPre(map);
            if (null == addupAmountPre) {
                oDomain.setAddupPreAmount(BigDecimal.ZERO);
            }
            oDomain.setAddupPreAmount(addupAmountPre);
            //下浮后
            BigDecimal addupAmountAfter = outcaeSureDetailDao.selectAddupAmountAfter(map);
            if (null == addupAmountAfter) {
                oDomain.setAddupAfterAmount(BigDecimal.ZERO);
            }
            oDomain.setAddupAfterAmount(addupAmountAfter);
            outcaeSureDetailDao.updateByIdSelective(oDomain);
            //操作主表
            OutcaeSure outcaeSure = outcaeSureDao.selectByPrimaryKey(oDomain.getIdBpa());
            //下浮前
            BigDecimal monthPreAmountPre = outcaeSureDao.selectMonthPreAmountPre(
                oDomain.getSureMonthBpa(), proCode);
            if (null == monthPreAmountPre) {
                outcaeSure.setAddupPreAmount(BigDecimal.ZERO);
            }
            outcaeSure.setAddupPreAmount(monthPreAmountPre);
            //下浮后
            BigDecimal monthAfterAmountAfter = outcaeSureDao.selectMonthAfterAmountAfter(
                oDomain.getSureMonthBpa(), proCode);
            if (null == monthAfterAmountAfter) {
                outcaeSure.setAddupAfterAmount(BigDecimal.ZERO);
            }
            outcaeSure.setAddupAfterAmount(monthAfterAmountAfter);
            outcaeSureDao.updateByIdSelective(outcaeSure);
        }
    }

}